package com.leetcode.first50;

/**
 * Created by weishaofei on 2018/11/25.
 */
public class Leetcode0006 {
    public String convert(String s, int numRows) {
        if (s == null || s.length() == 0 || s.length() <= numRows || numRows == 1) {
            return s;
        }
        int basicNum = 2 * numRows - 2;
        String result = "";
        char[] chars = s.toCharArray();
        for (int a = 0; a < numRows; a++) {
            if (a == 0 || a == basicNum / 2) {
                int index = a;
                while (index < s.length()) {
                    result += chars[index];
                    index += basicNum;
                }
                System.out.println(result);
                continue;
            }
            int m = a;
            int step = basicNum - 2 * a;
            while (m < chars.length) {
                result = result + chars[m];
                int n = m + step;
                if (n < chars.length) {
                    result += chars[n];
                }
                m += basicNum;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Leetcode0006 l6 = new Leetcode0006();
        String result = l6.convert("PAYPALISHIRING", 4);
        System.out.println(result);
        System.out.println(result.equals("PINALSIGYAHRPI"));
        System.out.println(l6.convert("PAYPALISHIRING", 3).equals("PAHNAPLSIIGYIR"));
    }
}
